import { Application } from "jsr:@oak/oak/application";
import { Router } from "jsr:@oak/oak/router";
import { walk } from "jsr:@std/fs/walk";

interface RouteModule {
  default?: Router;
}

export async function loadRoutes(app: Application) {
  const routesDir = new URL("../routes", import.meta.url);
  
  for await (const entry of walk(routesDir.pathname, {
    includeDirs: false,
    exts: [".ts"],
    skip: [/_/], // 跳过以_开头的文件
  })) {
    if (entry.isFile) {
      const module = await import(`file://${entry.path}`) as RouteModule;
      
      if (module.default && module.default instanceof Router) {
        app.use(module.default.routes());
        app.use(module.default.allowedMethods());
        
        console.log(`Loaded route: ${entry.path}`);
      }
    }
  }
} 